<template>
  <div class="recommendation-list">
    <!-- 标题和操作 -->
    <div class="list-header">
      <h3 class="list-title">推荐关注</h3>
      <div class="header-actions">
        <el-button
            type="primary"
            size="small"
            @click="handleRefresh"
            :loading="refreshing"
            class="refresh-btn"
        >
          <el-icon><Refresh /></el-icon>
          换一批
        </el-button>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-state">
      <el-icon class="loading-icon"><Loading /></el-icon>
      <span>推荐加载中...</span>
    </div>

    <!-- 空状态 -->
    <div v-else-if="recommendations.length === 0" class="empty-state">
      <el-empty description="暂无推荐用户">
        <span class="empty-text">暂时没有找到合适的推荐</span>
      </el-empty>
    </div>

    <!-- 推荐列表 -->
    <div v-else class="recommendations-grid">
      <div
          v-for="user in currentRecommendations"
          :key="user.id"
          class="recommendation-card"
      >
        <div class="card-header">
          <el-avatar
              :src="user.avatar"
              :size="50"
              class="user-avatar"
              @click="navigateToUser(user)"
          />
          <div class="user-basic-info">
            <h4 class="username" @click="navigateToUser(user)">{{ user.username }}</h4>
            <el-rate
                v-model="user.relevanceScore"
                :max="5"
                disabled
                show-score
                text-color="#ff9900"
                score-template="{value} 分"
                class="relevance-score"
            />
          </div>
        </div>

        <div class="card-content">
          <p class="user-signature">{{ user.description || '这个用户很神秘，什么都没有留下～' }}</p>

          <div class="user-stats">
            <div class="stat-item">
              <span class="stat-label">粉丝</span>
              <span class="stat-value">{{ formatCount(user.fansCount) }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">视频</span>
              <span class="stat-value">{{ formatCount(user.videoCount) }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">点赞</span>
              <span class="stat-value">{{ formatCount(user.likeCount) }}</span>
            </div>
          </div>

          <div class="tags-section">
            <el-tag
                v-for="tag in user.tags"
                :key="tag"
                size="small"
                type="info"
                class="user-tag"
            >
              {{ tag }}
            </el-tag>
          </div>
        </div>

        <div class="card-actions">
          <el-button
              type="primary"
              size="small"
              @click="handleFollow(user)"
              :loading="user.followLoading"
              class="follow-btn"
          >
            <el-icon><Plus /></el-icon>
            {{ user.isFollowing ? '已关注' : '关注' }}
          </el-button>

          <el-button
              size="small"
              @click="visitUserSpace(user)"
              class="visit-btn"
          >
            <el-icon><View /></el-icon>
            查看
          </el-button>
        </div>

        <div class="recommendation-reason">
          <el-icon><InfoFilled /></el-icon>
          <span>{{ getRecommendationReason(user) }}</span>
        </div>
      </div>
    </div>

    <!-- 加载更多 -->
    <div v-if="hasMore" class="load-more">
      <el-button
          type="text"
          @click="$emit('load-more')"
          :loading="loadingMore"
          class="load-more-btn"
      >
        <el-icon><ArrowDown /></el-icon>
        加载更多推荐
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  Refresh,
  Loading,
  Plus,
  View,
  InfoFilled,
  ArrowDown
} from '@element-plus/icons-vue'

// 定义Props
const props = defineProps({
  recommendations: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  },
  hasMore: {
    type: Boolean,
    default: false
  },
  loadingMore: {
    type: Boolean,
    default: false
  }
})

// 定义Emits
const emit = defineEmits(['follow', 'refresh', 'load-more'])

const router = useRouter()
const refreshing = ref(false)
const currentBatch = ref(0)
const batchesPerPage = 4

// 计算当前显示的建议
const currentRecommendations = computed(() => {
  const start = currentBatch.value * batchesPerPage
  const end = start + batchesPerPage
  return props.recommendations.slice(start, end)
})

// 处理方法
const handleFollow = (user) => {
  emit('follow', user)
}

const handleRefresh = async () => {
  refreshing.value = true
  try {
    // 循环批次
    currentBatch.value = (currentBatch.value + 1) % Math.ceil(props.recommendations.length / batchesPerPage)
    await emit('refresh')
    ElMessage.success('推荐已更新')
  } catch (error) {
    ElMessage.error('更新失败，请重试')
  } finally {
    refreshing.value = false
  }
}

const navigateToUser = (user) => {
  router.push(`/user/${user.id}`)
}

const visitUserSpace = (user) => {
  window.open(`/user/${user.id}`, '_blank')
}

// 工具函数
const formatCount = (count) => {
  if (!count) return '0'
  if (count >= 10000) {
    return (count / 10000).toFixed(1) + '万'
  }
  return count.toString()
}

const getRecommendationReason = (user) => {
  const reasons = [
    '与您有共同关注',
    '相似兴趣领域',
    '热门内容创作者',
    '高质量内容产出',
    '同城推荐'
  ]

  return user.recommendReason || reasons[Math.floor(Math.random() * reasons.length)]
}
</script>

<style scoped>
.recommendation-list {
  padding: 20px;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.list-title {
  margin: 0;
  color: #18191c;
  font-size: 18px;
  font-weight: 600;
}

.refresh-btn {
  background: linear-gradient(135deg, #00a1d6, #00c4cc);
  border: none;
}

.recommendations-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.recommendation-card {
  background: #fff;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  border: 1px solid #f5f5f5;
}

.recommendation-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.user-avatar {
  cursor: pointer;
  transition: transform 0.2s;
  border: 2px solid #f0f0f0;
}

.user-avatar:hover {
  transform: scale(1.1);
  border-color: #00a1d6;
}

.user-basic-info {
  flex: 1;
}

.username {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 600;
  color: #18191c;
  cursor: pointer;
  transition: color 0.2s;
}

.username:hover {
  color: #00a1d6;
}

.relevance-score {
  font-size: 12px;
}

.card-content {
  margin-bottom: 16px;
}

.user-signature {
  margin: 0 0 12px 0;
  font-size: 14px;
  color: #61666d;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.user-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.stat-item {
  text-align: center;
  padding: 6px;
  background: #f8f9fa;
  border-radius: 6px;
}

.stat-label {
  display: block;
  font-size: 10px;
  color: #999;
  margin-bottom: 2px;
}

.stat-value {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: #00a1d6;
}

.tags-section {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.user-tag {
  font-size: 10px;
  padding: 1px 6px;
}

.card-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.follow-btn {
  flex: 2;
  background: linear-gradient(135deg, #00a1d6, #00c4cc);
  border: none;
}

.visit-btn {
  flex: 1;
  border-color: #dcdfe6;
}

.visit-btn:hover {
  border-color: #00a1d6;
  color: #00a1d6;
}

.recommendation-reason {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #999;
  padding: 6px 0;
  border-top: 1px dashed #f0f0f0;
}

.recommendation-reason .el-icon {
  color: #00a1d6;
}

.load-more {
  text-align: center;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
}

.load-more-btn {
  color: #00a1d6;
  font-size: 14px;
}

.load-more-btn:hover {
  color: #009bd4;
}

.loading-state, .empty-state {
  padding: 40px 20px;
  text-align: center;
}

.loading-icon {
  font-size: 32px;
  margin-bottom: 12px;
  animation: spin 1s linear infinite;
}

.empty-text {
  display: block;
  margin-bottom: 16px;
  color: #999;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .recommendation-list {
    padding: 12px;
  }

  .list-header {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }

  .recommendations-grid {
    grid-template-columns: 1fr;
  }

  .user-stats {
    grid-template-columns: repeat(3, 1fr);
  }

  .card-actions {
    flex-direction: column;
  }
}
</style>